﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceGame
{
    public class MovingObject : GameObject, IMovable
    {
        protected int speed;
        private Position direction;

        protected int updatedCounter;

        public MovingObject(Position topLeft, char[,] image, int speed, Position dir)
            : base(topLeft, image)
        {
            this.Speed = speed;
            this.Direction = dir;
            updatedCounter = 0;
        }

        public int Speed
        {
            get
            {
                return this.speed;
            }
            set
            {
                this.speed = value;
            }
        }

        public override void Update()
        {
            if (updatedCounter++ % speed == 0)
            {
                this.UpdatePosition();
            }
        }

        public Position Direction
        {
            get
            {
                return this.direction;
            }
            set
            {
                this.direction = value;
            }
        }

        public virtual void UpdatePosition()
        {
            Position newPosition = this.TopLeft + direction;

            if (newPosition.Row < 0 || newPosition.Col < 0)
            {
                this.IsDestroyed = true;
            }
            else
            {
                this.TopLeft = newPosition;
            }
        }
    }
}
